#include "FreeRTOS.h"        //RTOS主文件
#include "task.h"            //RTOS任务管理
#include "queue.h"           //RTOS队列管理
#include "semphr.h"          //RTOS消息管理
#include "event_groups.h"    //RTOS事件管理
#include "timers.h"          //RTOS定时管理

#include "./Driver/Debug_Driver/debug_driver.h"

#include "./APP/App_io_ctrl/app_io_ctrl.h"
#include "./APP/App_button/app_button.h"
#include "./APP/App_gui/app_oled.h"
#include "./BSP/WWDG_BSP/wwdg_bsp.h"

#include "./APP/App_ModBus/app_modbus.h"
#include "./APP/App_lvgl/app_lvgl.h"

WHT_WWDG_Config_t WWDG_Config = {36000, 10, 50, ENABLE};

/* M3中断优先级分组为4，即4bit都用来表示抢占优先级，范围为：0~15 */
static void Hardware_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO ,ENABLE);//重映射使能AFIO时钟
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//只关闭JTAG而保留SWD
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO ,DISABLE);//重映射失能AFIO时钟
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    WHT_Debug_Driver.WHT_Init();//debug串口初始化

    //WHT_WWDG_BSP.WHT_Config(&WWDG_Config);
}



static void App_Start_Task(void* parameter);//启动其它任务的启动任务
static void Admin_Task(void* parameter);    //管理其它任务的管理任务
static void WHT_Test_Task(void* parameter); //测试任务

/**********************************************************************/
int main(void)
{
    Hardware_Init();//先初始化硬件
    /*             任务函数         任务名字    任务堆栈大小 任务传参 任务优先级    任务控制块 */
    xTaskCreate(App_Start_Task, "App_Start_Task", 256, NULL, configMAX_PRIORITIES, NULL);
    vTaskStartScheduler();
    while(1);
}
/**********************************************************************/
static void App_Start_Task(void *parameter)
{
    printf("SystemCoreClock = %uMHz\r\n", SystemCoreClock / 1000000);
    for (uint8_t Start_Countdown_Time = 4; Start_Countdown_Time > 0; Start_Countdown_Time--)
    {
        printf("STM32F103 Start Countdown is %d Sec\r\n", Start_Countdown_Time - 1);
        vTaskDelay(1000 / portTICK_RATE_MS);
    }
    printf("*************************************\r\n");

    taskENTER_CRITICAL();
    
    xTaskCreate(Admin_Task, "Admin_Task", 320, NULL, 1, NULL);
    xTaskCreate(WHT_Test_Task, "WHT_Test_Task", 320, NULL, 3, NULL);
    
    /*************添加其它任务代码**************/
    //WHT_Button_t WHT_Button;
    //WHT_Button_Init(5, &WHT_Button);
    //WHT_OLED_Install(3);
    //WHT_ModBus_Install(6);
    //WHT_LVGL_Install(0);
    /*************添加其它任务代码**************/

    taskEXIT_CRITICAL();
	vTaskDelete(NULL);
}
/**********************************************************************/


/*RTOS任务实时状态任务*/
static void Admin_Task(void* parameter)
{
    char Task_buf[512];

    for(;;)
    {
        vTaskList(Task_buf);
        WHT_Debug_Driver.WHT_Printf("Task_Name     State  Priority  Stack  Number\r\n", 46);//格局已调试OK
        WHT_Debug_Driver.WHT_Printf(Task_buf, strlen(Task_buf));
        WHT_Debug_Driver.WHT_Printf("\r\n", 2);
        vTaskDelay(3000 / portTICK_RATE_MS);
    }
}



/*Debug调试任务*/
static void WHT_Test_Task(void* parameter)
{
    uint8_t receive_buff[128];
    uint16_t Length;

    for (; ;)
    {
        Length = WHT_Debug_Driver.WHT_Scanf(receive_buff, sizeof(receive_buff), portMAX_DELAY);
        printf("\r\nreceive %d\r\n", Length);
        WHT_Debug_Driver.WHT_Printf((char*)receive_buff, Length);//原样发出
    }
}
